﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Clipper.Model;
using System.Threading;

using Global;
using Communication;
using Clipper.Model.EnumModel;

namespace Clipper.BLL.DetectChain
{
    /// <summary>
    /// 加底物
    /// </summary>
    public class AddSubstrate:ApplianceComponent
    {
        private static Arm mArmAS = new Arm("AS", "11");                // 加底物臂
        private static ControlStatusEnum mStatusAS = ControlStatusEnum.IDLE;    // 加底物臂的状态

        /// <summary>
        /// 加底物臂
        /// </summary>
        public static Arm ArmAS
        {
            get { return mArmAS; }
            set { mArmAS = value; }
        }

        #region 变量和属性

        /// <summary>
        /// 底物瓶剩余底物量
        /// </summary>
        //static int SubstrateLeft;

        static ControlStatusEnum detectionStatus;
        /// <summary>
        /// 状态
        /// </summary>
        public override ControlStatusEnum DetectionStatus
        {
            get
            {
                return detectionStatus;
            }
            set
            {
                detectionStatus = value;
            }
        }
        static AutoResetEvent mPause = new AutoResetEvent(false);//暂停
        static bool mIsPause;
        /// <summary>
        /// 获得或设置模块暂停状态
        /// </summary>
        public static bool IsPause
        {
            get
            {
                return mIsPause;
            }
        }
        /// <summary>
        /// 底物旋臂
        /// </summary>
        public static SubstrateControl Arm = new SubstrateControl();
        /// <summary>
        /// 底物泵
        /// </summary>
        public static SubstrateControl Pump = new SubstrateControl();

        #endregion

        public AddSubstrate()
        {
            PositionCode = 75;
        }
        public override void Run(ReactionCup strip)
        {
            //int substrateCount = 3110;
            //detectionStatus = ControlStatusEnum.BUSY;
            //strip.ReactionStripSeat = ReactionStripStageEnum.ADDSUBSTRATE;
            //ServiceProvider.Monitor.SubPosition = strip;
            //DetectChainHelper.Position(PositionCode);
            //strip.Note = this.GetType().Name + Thread.CurrentThread.Name + "---" + strip.ID+"------";
            //Console.WriteLine(strip.Note);
            //Arm.WaitStatus(Arm.ArmPosition());
            //if (ServiceProvider.IsSimulate)
            //{
            //    Simulate(strip);
            //}
            //else
            //{
            //    for (int i = 0; i < strip.ReactionCupList.Count; i++)
            //    {
            //        if (i < 4)
            //        {
                       
            //            Transportation.TransportC.LeftPosition();
            //            Transportation.TransportC.WaitStatus(1); 
            //            Pump.PumpCharge(substrateCount);
            //            Pump.WaitStatus(4);
            //            Arm.WaitStatus(Arm.ArmAddSubstrate(substrateCount));
            //        }
            //        else
            //        {
            //            Transportation.TransportC.RightPosition();
            //            Transportation.TransportC.WaitStatus(2);
            //            Pump.PumpCharge(substrateCount);
            //            Pump.WaitStatus(4);
            //            Arm.WaitStatus(Arm.ArmAddSubstrate(substrateCount));
            //        }
            //    }
            //}
            //AddSubstrate.Arm.ArmReset();
            //AddSubstrate.Arm.WaitStatus(0);//旋臂复位
            //AddSubstrate.Pump.PumpReset();//复位泵
            //AddSubstrate.Pump.WaitStatus(3);//确定泵复位

            //Transportation.AddTransportation(strip, this, NextComponent);
        }
        public static void Engineer()
        {
            Pump.WaitStatus(3);//确定泵复位
            Pump.PumpCharge(3110);
            Pump.WaitStatus(4);//等待吸底物
            Transportation.TransportC.LeftPosition();
            Transportation.TransportC.WaitStatus(1);
            Arm.WaitStatus(Arm.ArmPosition());
            for (int i = 0; i < 4; i++)
            {
                Arm.WaitStatus(Arm.ArmAddSubstrate(3110));
                if (i < 3)//第四个不使用此命令
                {
                    Transportation.TransportC.LeftPosition();
                    Transportation.TransportC.WaitStatus(1);
                }
            }
            for (int i = 0; i < 4; i++)
            {
                Transportation.TransportC.RightPosition();
                Transportation.TransportC.WaitStatus(2);

                Arm.WaitStatus(Arm.ArmPosition());
                Arm.WaitStatus(Arm.ArmAddSubstrate());
            }
            Transportation.TransportC.WaitStatus(Transportation.TransportC.GoIncubation());
        }
        public static void Reset()
        {
            //Transportation.TransportC.Home();
            Arm.ArmReset();
            Arm.WaitStatus(0);
           
            Pump.WaitStatus(Pump.PumpReset());
            //Transportation.TransportC.WaitStatus(0);
        }
        /// <summary>
        /// 暂停/恢复
        /// </summary>
        /// <param name="isPause">暂停true,恢复false</param>
        public static void Pause(bool isPause)
        {
            mIsPause = isPause;
            Arm.IsPause = isPause;
            Pump.IsPause = isPause;
        }
        public override void Simulate(ReactionCup strip)
        {

            do
            {
                if (IsPause)//局部暂停
                {
                    mPause.WaitOne();
                }
                if (ServiceProvider.ReagentKitVM.SubstrateInfo.substrateAmount < 100)
                {
                    //错误
                    //if (ServiceProvider.Monitor.SubstrateState != ComponentStateEnum.ERROR)
                    //{
                        DetectChainHelper.PostMessage(3002);
                        ServiceProvider.Monitor.SubstrateState = ComponentStateEnum.ERROR;
                        //IsPause = true;//暂停
                        mPause.WaitOne();
                    //}
                }
                else
                {
                    //警告
                    if (ServiceProvider.ReagentKitVM.SubstrateInfo.substrateAmount < 1000 &&
                        ServiceProvider.ReagentKitVM.SubstrateInfo.substrateAmount > 100)
                    {
                        if (ServiceProvider.Monitor.SubstrateState != ComponentStateEnum.ALERT)
                        {
                            DetectChainHelper.PostMessage(2002);
                        }
                        ServiceProvider.Monitor.SubstrateState = ComponentStateEnum.ALERT;
                        Thread.Sleep(ServiceProvider.SubstractSleepTime);
                    }
                    else
                    {
                        //正常
                        ServiceProvider.Monitor.SubstrateState = ComponentStateEnum.NORMAL;
                        Thread.Sleep(ServiceProvider.SubstractSleepTime);
                    }
                }
            } while (ServiceProvider.Monitor.SubstrateState == ComponentStateEnum.ERROR);
        }


        #region // 模块运行控制，Anry
        private void RunControl(ReactionStrip strip)
        {
            for (int i = 0; i < strip.ReactionCupList.Count; i++)
            {
                // 如果模块需要暂停，则暂停模块运行，等待外部改变此状态且继续模块运行
                if (IsPause)
                {
                    mPause.WaitOne();
                }

                if (ServiceProvider.gStatusSlideC == ControlStatusEnum.IDLE)
                {
                    Transportation.AssayCupFeeding('C', (byte)(i + 1));
                    ServiceProvider.gStatusSlideC = ControlStatusEnum.BUSY;
                }

                // 循环等待，直到滑轨C状态为空闲，跳出循环
                while (ServiceProvider.gStatusSlideC == ControlStatusEnum.BUSY)
                {
                    System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
                }

                // 如果模块需要暂停，则暂停模块运行，等待外部改变此状态且继续模块运行
                if (IsPause)
                {
                    mPause.WaitOne();
                }

                // 如果加底物臂空闲
                if (mStatusAS == ControlStatusEnum.IDLE)
                {
                    // 如果底物的剩余量够用
                    mArmAS.AddSubstrate();
                    mStatusAS = ControlStatusEnum.BUSY;
                }

                // 循环等待，直到滑轨D状态为空闲，跳出循环
                while (mStatusAS == ControlStatusEnum.BUSY)
                {
                    System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
                }
            }
        }
        #endregion // 模块运行控制，Anry
    }
}
